<?php

namespace app\data\controller;

use app\data\controller\base\Publics;
use think\Db;
use think\Log;

/**
 * 终端交易
 * Class
 * @package app\data\controller
 */
class Trans
{

	/**
	 * 实例对象
	 * @var object
	 */
	protected static $instance;

	/**
	 * 实例化
	 */
	public static function instance()
	{
		if (is_null(self::$instance)) {
			self::$instance = new static();
		}
		return self::$instance;
	}

	/**
	 * 客户终端月交易统计
	 * @param string $sn 终端编号
	 * @param float|double $money  交易时间
	 * @param int 	$times  交易时间
	 */
	public function customer_month($sn, $money, $times)
	{
		try {
			$option = [];
			if ($sn && $money && $times) {
				$option = [
					"sn" => $sn,
					"money" => $money,
					"times" => strtotime(date("Y-m", $times))
				];
				$binds = Publics::instance()->snBinds($sn);
				if ($binds) {
					if ($binds["uid"]) {
						$option["uid"] = $binds["uid"];
						$option["proid"] = $binds["proid"];
						$this->save($option);
						$this->save_customer_trans_days($option["uid"], $money, $times);
						
						//  新人有礼活动达标金额计算
						\app\data\controller\Activity::instance()->newlyTakes($sn, $money, $times);

						// 激活达标信息 - 交易存储
						$this->active_stands_trans_store($sn, $times, $money);
						// 客户交易存储
						$this->losts_customer_trans_store($option["uid"], $times, $money);
					}
				}
			}
		} catch (\Exception $e) {
			Log::write($e->getMessage());
		}
	}

	/** 
	 * 客户交易存储 - 累计
	 */
	public function save_customer_trans_days($uid, $money, $times)
	{
		try {
			$where = [];
			$table = Publics::instance()->tab()->cus_tran_day;
			$times = strtotime(date("Y-m-d", $times));
			$where = ["uid" => $uid, "times" => $times];
			$checkStore = Db::name($table)->where($where)->count();
			if ($checkStore) {
				$update = [
					"nums" => Db::raw("nums + 1"),
					"money" => Db::raw("money + " . $money)
				];
				Db::name($table)->where($where)->update($update);
			} else {
				$data = [
					"uid" => $uid,
					"money" => $money,
					"nums" => 1,
					"times" => $times,
					"createtime" => time()
				];
				Db::name($table)->insert($data);
			}
		} catch (\Exception $e) {
			Log::write($e->getMessage());
		}
	}

	/**
	 * 更新
	 */
	public function save($option)
	{
		try {
			if (is_array($option) && $option) {
				$ret = false;
				$where = [];
				$tab = Publics::instance()->tab();
				$where = ["sn" => $option["sn"], "times" => $option["times"]];
				$checkStore = Db::name($tab->tran_mon)->where($where)->count();
				if ($checkStore) {
					$update = [
						"nums" => Db::raw("nums + 1"),
						"money" => Db::raw("money + " . $option['money'])
					];
					$ret = Db::name($tab->tran_mon)->where($where)->update($update);
				} else {
					$option["nums"] = 1;
					$option["createtime"] = time();
					$ret = Db::name($tab->tran_mon)->insert($option);
				}
				if ($ret) {
					$limitVal = config("nums.warnNo");
					$tranAmt = Db::name($tab->tran_mon)->where($where)->value("money");
					if (is_numeric($limitVal)) {
						if (intval($limitVal) < intval($tranAmt)) {
							// 存储超标客户
							$checkWarnStore = Db::name($tab->tran_warn)
								->where(["uid" => $option["uid"], "times" => $option['times']])
								->count();
							if (!$checkWarnStore) {
								$data = [
									"uid" => $option["uid"],
									"times" => $option["times"],
									"limitVal" => $limitVal,
									"create_time" => time()
								];
								$ret = Db::name($tab->tran_warn)->insert($data);
								if ($ret) {
									$this->store_apply_auth($data["uid"]);
								}
							}
						}
					}
				}
			}
		} catch (\Exception $e) {
			Log::write($e->getMessage());
		}
	}

	/**
	 * 存储申领权限
	 */
	public function store_apply_auth($uid)
	{
		$where = [];
		$table = Publics::instance()->tab()->apply;
		$where = ["uid" => $uid, "status" => 1];
		$find =	Db::name($table)->where($where)->find();
		if ($find) {
			if ($find["is_apply"] == 2) {
				$update = ["is_apply" => 1, "update_time" => time()];
				Db::name($table)->where($where)->update($update);
			}
		} else {
			$data = ["uid" => $uid, "is_apply" => 1, "create_time" => time()];
			Db::name($table)->insert($data);
		}
	}

	/**
	 * 客户交易存储
	 */
	public function losts_customer_trans_store($uid, $times, $money)
	{
		try {
			$where = "uid = " . $uid;
			$update["last_time"] = $times;
			$update["money"] = Db::raw("money + " . $money);
			Db::name(Publics::instance()->tab()->lost)->where($where)->update($update);
		} catch (\Exception $e) {
			Log::write($e->getMessage());
		}
	}

	/**
	 * 激活达标信息 - 交易存储
	 */
	public function active_stands_trans_store($sn, $times, $money)
	{
		try {
			$finds = Publics::instance()->get_active_stands_sn($sn);
			if ($finds) {
				$table = Publics::instance()->tab()->acte_stands;
				if ($finds["stands_typs"] == 1) {
					if ($times >= $finds['bind_times'] && $times <= $finds["end_times"]) {
						$update["money"] = Db::raw("money + " . $money);
						$ret = Db::name($table)->where("sn = '{$sn}'")->update($update);
						if ($ret) {
							$finds = Publics::instance()->get_active_stands_sn($sn);
							if ($finds["money"] >= $finds['stands_money']) {
								Db::name($table)->where("sn = '{$sn}'")->update(["stands_typs" => 2]);
							}
						}
					}
				}
			}
		} catch (\Exception $e) {
			Log::write($e->getMessage());
		}
	}

	/**
	 * 终端 - 商户绑定之前交易获取
	 */
	public function sn_active_before_trans($table, $sn, $start, $end)
	{
		$sum = "0.00";
		if ($table && $sn && $start && $end) {
			$where = "";
			$where = "sn = '{$sn}' and trading_time < " . strtotime("+1 day", $start);
			$checkTrans = Db::name($table)->where($where)->count();
			if ($checkTrans) {
				$sum = Db::name($table)->where($where)->sum('transaction_amount');
			} else {
				$where = "sn = '{$sn}' and ( trading_time >= {$start} and trading_time < " . strtotime("+1 day", $end) . ")";
				$checkTrans = Db::name($table)->where($where)->count();
				if ($checkTrans) {
					$sum = Db::name($table)->where($where)->sum('transaction_amount');
				}
			}
		}
		return $sum;
	}
}
